package com.btf.push;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.VCard;

import com.butterfly.vv.model.Contact;
import com.butterfly.vv.vv.utils.Debug;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
public class VcardListener implements PacketListener {
	
	Handler handler = null;
	
	public VcardListener(Handler handler) {
		// TODO Auto-generated constructor stub
		this.handler = handler;
	}
	
	public VcardListener() {
		// TODO Auto-generated constructor stub
		
	}

	@Override
	public void processPacket(Packet arg0) {
		// TODO Auto-generated method stub
		//System.out.println("--*VcardListener"+arg0.toXML());
		Log.i("VV",( arg0 instanceof AddVCard )+"--*VcardListener"+arg0.toXML());
		if (arg0 instanceof AddVCard ) {
			if( ( (AddVCard)arg0).getType()==Type.RESULT){
			AddVCard addVCard = (AddVCard) arg0;
			Log.i("VV", "gg "+addVCard.getSchoolname());
			}
		}
		if (arg0 instanceof VCard) {
			VCard card = (VCard) arg0;
			//System.out.println(card.toString());
			//System.out.println("**"+card.getAvatar());
			Message msg = new Message();
			Bundle data = new Bundle();
			data.putString("myname", card.getNickName());
			data.putByteArray("avatar", card.getAvatar());
			msg.setData(data);
			msg.what = 4;
			//20120125handler.sendMessage(msg);
		}
//		if (arg0 instanceof CityVCard) {
//			CityVCard gpsPacket =  (CityVCard) arg0;
//			System.err.println(""+gpsPacket.getRosterItemCount()+gpsPacket.toXML());
//			Collection<com.btf.push.CityVCard.Item> items = gpsPacket.getRosterItems();
//			ArrayList<com.beem.project.beem.service.Contact> contacts = new ArrayList<com.beem.project.beem.service.Contact>();
//    		ArrayList<com.teleca.jamendo.api.Contact> vvcontacts = new ArrayList<com.teleca.jamendo.api.Contact>();
//
//			for (Iterator iterator = items.iterator(); iterator.hasNext();) {
//				com.btf.push.CityVCard.Item item = (com.btf.push.CityVCard.Item) iterator.next();
//				com.beem.project.beem.service.Contact contact = new com.beem.project.beem.service.Contact(item.getUser());
//				contact.setBday(item.getBday());
//				contact.setRole(item.getRole());
//				contact.setName(item.getUser());
//				contact.setPhoto(StringUtils.encodeBase64(item.getPhoto()));
//				
//				com.teleca.jamendo.api.Contact c = new com.teleca.jamendo.api.Contact();
//				c.setBday(item.getBday());
//				c.setRole(item.getRole());
//				c.setmName(item.getUser());
//				c.setPhoto(StringUtils.encodeBase64(item.getPhoto()));
//				vvcontacts.add(c);
//
//				Debug.getDebugInstance().log("processPacket "+item.toXML()+" "+item.getBday()+item.getRole()+item.getPhoto());
//				//contact.setDistance(item.getDistance());
//				contacts.add(contact);
//			}
//			Message msg = new Message();
//			Bundle data = new Bundle();
//			
//			data.putParcelableArrayList("cityvvcontacts",  contacts);
//			msg.setData(data);
//			msg.what = 3;
//			handler.sendMessage(msg);
////			for (Iterator iterator = items.iterator(); iterator
////					.hasNext();) {
////				com.btf.push.GPSPacket.Item item = (com.btf.push.GPSPacket.Item) iterator.next();
////			}
//		}
	}

}
